<?php

class binovaChartMng {
  
  public $width;
  public $height;
  public $fixedMax;
  public $fontname;
  public $fontlocation;
  public $fontSize;
  public $titleFontSize;
  
  public function __construct($config = array()) {
   	$this->width = isset($config["width"]) ? $config["width"] : 700;
  	$this->height = isset($config["height"]) ? $config["height"] : 500;
  	$this->fontname = isset($config["fontName"]) ? $config["fontName"] : sfConfig::get('app_reports_defaultfont', 'calibri'); //
  	$this->fontlocation = sfConfig::get('app_reports_fontslocation', 'css/fonts/');	
  	$this->fontSize = isset($config["fontSize"]) ? $config["fontSize"] : sfConfig::get('app_reports_defaultfontsize', 10); //
  	$this->titleFontSize = isset($config["titleFontSize"]) ? $config["titleFontSize"] : sfConfig::get('app_reports_defaulttitlefontsize', 10); //
  	$this->fixedMax =  isset($config["fixedMax"]) ? $config["fixedMax"] : VOID;
  }
  

  public function getNoDataImg(){
		$myPicture = new pImage($this->width,$this->height,$MyData);

		
		 /* Turn of Antialiasing */
		$myPicture->Antialias = FALSE;

		/* Add a border to the picture */
		$myPicture->drawRectangle(0,0,$this->width-1,$this->height-1,array("R"=>0,"G"=>0,"B"=>0));

 		/* Set the default font */
		$fontname = $this->fontname;
		$myPicture->setFontProperties(array("FontName"=> $this->fontlocation . $this->fontname. '.ttf',"FontSize"=>$this->titleFontSize));
		$i18n = sfContext::getInstance()->getI18N();
		$myPicture->drawText(5,15,$i18n->__('No data found for your report'));///$graphInfo['title'] 
		
		$myPicture->drawFromPNG($this->width/3,$this->height / 3,"images/report_no_data.png"); 
		$myPicture->autoOutput("pictures/example.drawBarChart.simple.png");
		
  }
  /*
   * Returns chart image
   * @params array $data 
   * 
   * $graphInfo['title']  => Graph Title
   * $graphInfo[data]
   * 
   * 
   * */
  
  public function createVerticalBarChart($graphInfo){
 		/* Create and populate the pData object */
 		$MyData = new pData();
 		//$MyData->addPoints(array(150,220,300,-250,-420,-200,300,200,100),"Server A");
 		//$MyData->addPoints(array(140,0,340,-300,-320,-300,200,100,50),"Server B");
 		foreach ($graphInfo['data'] as $key => $serie){
 			$MyData->addPoints($serie['values'],$serie['label']);
 		}
 		
 		$MyData->setAxisName(0,$graphInfo['y_legend']);
 		$MyData->addPoints($graphInfo['labels'],$graphInfo['y_legend']);
 		//$MyData->setSerieDescription("Months","Month");
 		
 		//$MyData->setAbscissa("Months");
 		$MyData->setAbscissa($graphInfo['y_legend']);
		 /* Create the pChart object */
		$myPicture = new pImage($this->width,$this->height,$MyData);

		
		 /* Turn of Antialiasing */
		$myPicture->Antialias = FALSE;

		/* Add a border to the picture */
		$myPicture->drawRectangle(0,0,$this->width-1,$this->height-1,array("R"=>0,"G"=>0,"B"=>0));

 		/* Set the default font */
		$fontname = $this->fontname;
		$myPicture->setFontProperties(array("FontName"=> $this->fontlocation . $this->fontname. '.ttf',"FontSize"=>$this->titleFontSize));
		
		$myPicture->drawText(5,15,$graphInfo['title']);///$graphInfo['title'] 
		
		$myPicture->setFontProperties(array("FontName"=> $this->fontlocation . $this->fontname. '.ttf',"FontSize"=>$this->fontSize));

 		
 		/* Define the chart area */
 		$myPicture->setGraphArea(50,50,$this->width - 50,$this->height-50);

 		/* Draw the scale */
 		$scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE, "LabelRotation"=>45);
 		$myPicture->drawScale($scaleSettings);

		/* Write the chart legend */
		$myPicture->drawLegend(5,25,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
		
		/* Turn on shadow computing */ 
		$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
		
		/* Draw the chart */
		$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
		$settings = array("Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>255,"DisplayG"=>255,"DisplayB"=>255,"DisplayShadow"=>TRUE,"Surrounding"=>10);
		$settings = array("DisplayOrientation"=>ORIENTATION_VERTICAL,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30);
		$myPicture->drawBarChart($settings);
		

		/* Render the picture (choose the best way) */
		$myPicture->autoOutput("pictures/example.drawBarChart.simple.png");
  }

  /*
   * Returns chart image
   * @params array $data 
   * 
   * $graphInfo['title']  => Graph Title
   * $graphInfo[data]
   * 
   * 
   * */
  
  public function createAreaChart($graphInfo){
	
	 /* Create and populate the pData object */
	 $MyData = new pData();   
     foreach ($graphInfo['data'] as $key => $serie){
 		$MyData->addPoints($serie['values'],$key);
 		$MyData->setSerieDescription($key,$serie['label']);
	 	$MyData->setSerieTicks($key,4);
 	 }
 
/*	 $MyData->addPoints(array(4,2,10,12,8,3),"Probe 1");
	 $MyData->addPoints(array(3,12,15,8,5,5),"Probe 2");
	 $MyData->addPoints(array(2,7,5,18,15,22),"Probe 3");
	 $MyData->setSerieTicks("Probe 2",4);
	 */
 	 $MyData->setAxisName(0,'Test!');
 	 $MyData->addPoints($graphInfo['labels'],'Labels');
 	 $MyData->setSerieDescription('Labels',$graphInfo['y_legend']);
 		
 		//$MyData->setAbscissa("Months");
 	 $MyData->setAbscissa('Labels');
 	 
	 /*$MyData->setAxisName(0,"Temperatures");
	 $MyData->addPoints(array("Jan","Feb","Mar","Apr","May","Jun"),"Labels");
	 $MyData->setSerieDescription("Labels","Months");
	 $MyData->setAbscissa("Labels");
	 */
	 /* Create the pChart object */
	 //$myPicture = new pImage(700,230,$MyData);
	 $myPicture = new pImage($this->width,$this->height,$MyData);
	 
	 /* Turn of Antialiasing */
	 $myPicture->Antialias = FALSE;
	
	 /* Add a border to the picture */
	// $myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));
	 $myPicture->drawRectangle(0,0,$this->width - 1, $this->height-1 ,array("R"=>0,"G"=>0,"B"=>0));
	 
 	 $this->drawTitle($myPicture, 10,13,$graphInfo['title'], array("R"=>255,"G"=>255,"B"=>255));
 
	 /* Write the chart title */ 
	 /*$myPicture->setFontProperties(array("FontName"=>$this->fontlocation . $this->fontname. '.ttf',"FontSize"=>11));
	 $myPicture->drawText(150,35,"Average temperature",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
	 */
	 
	 /* Set the default font */
	 $myPicture->setFontProperties(array("FontName"=>$this->fontlocation . $this->fontname. '.ttf',"FontSize"=>$this->fontSize));
	
	 /* Define the chart area */
	 //$myPicture->setGraphArea(60,40,650,200);
	 $myPicture->setGraphArea(30,25,$this->width - ($this->width * 0.02), $this->height - ($this->height * 0.15) );
	 
	 /* Draw the scale */
	 $scaleSettings = array("XMargin"=>10,"YMargin"=>10,"Floating"=>TRUE,"GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
	 $myPicture->drawScale($scaleSettings);
	
	 /* Write the chart legend */
	 $myPicture->drawLegend(10 ,$this->height - ($this->height * 0.1),array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
	
	 /* Turn on Antialiasing */
	 $myPicture->Antialias = TRUE;
	
	 /* Draw the area chart */
	 $myPicture->drawAreaChart();
	
	 /* Render the picture (choose the best way) */
	 $myPicture->autoOutput("pictures/example.drawAreaChart.simple.png"); 
  }
  
    public function createRadarChart($graphInfo){
		/* Create and populate the pData object */
    	
		 $MyData = new pData();   
	     foreach ($graphInfo['data'] as $key => $serie){
	 		$MyData->addPoints($serie['values'],$key);
	 		$MyData->setSerieDescription($key,$serie['label']);
		 	$MyData->setPalette($key,array("R"=>157,"G"=>196,"B"=>22));
	 	 }
		 //$MyData->addPoints(array(8,4,6,4,2,7),"Score");  
		 //$MyData->setSerieDescription("Score","Application A");
		 //$MyData->setPalette("Score",array("R"=>157,"G"=>196,"B"=>22));
	 	 
		 /* Define the absissa serie */
		foreach ($graphInfo['categories'] as $key => $categorie){
	 		$MyData->addPoints($categorie['values'],$categorie['label']);
	 		 $MyData->setAbscissa($categorie['label']);
	 	 }

	 	 $myPicture = new pImage($this->width,$this->height,$MyData);
		 $myPicture->drawGradientArea(0,0,$this->width,$this->height,DIRECTION_VERTICAL,array("StartR"=>200,"StartG"=>200,"StartB"=>200,"EndR"=>240,"EndG"=>240,"EndB"=>240,"Alpha"=>100));
		 $myPicture->drawGradientArea(0,0,$this->width,20,DIRECTION_HORIZONTAL,array("StartR"=>30,"StartG"=>30,"StartB"=>30,"EndR"=>100,"EndG"=>100,"EndB"=>100,"Alpha"=>100));
		 $myPicture->drawLine(0,20,$this->width,20,array("R"=>255,"G"=>255,"B"=>255));
		 
		 /* Add a border to the picture */
		 $myPicture->drawRectangle(0,0,$this->width - 1, $this->height-1 ,array("R"=>0,"G"=>0,"B"=>0));
		 
		 $this->drawTitle($myPicture, 10,13,$graphInfo['title'], array("R"=>255,"G"=>255,"B"=>255));
		 /* Set the default font properties */ 
		 $myPicture->setFontProperties(array("FontName"=>$this->fontlocation . $this->fontname. '.ttf',"FontSize"=>$this->fontSize,"R"=>80,"G"=>80,"B"=>80));
		
		 /* Enable shadow computing */ 
		 $myPicture->setShadow(TRUE,array("X"=>2,"Y"=>2,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
		
		 /* Create the pRadar object */ 
		 $SplitChart = new pRadar();
		
		 /* Draw a radar chart */ 
		 $myPicture->setGraphArea(10,25,$this->width - ($this->width * 0.05), $this->height - ($this->height * 0.05) );
		 $Options = array("DrawPoly"=>TRUE,"WriteValues"=>TRUE, "ValueFontSize"=>5, "Layout"=>RADAR_LAYOUT_CIRCLE,
		 				"ValueFontSize"=>5, "PointRadius" => 2, "FixedMax" => $this->fixedMax,
		 				"BackgroundGradient"=>array("StartR"=>255,"StartG"=>255,"StartB"=>255,"StartAlpha"=>100,"EndR"=>207,"EndG"=>227,"EndB"=>125,"EndAlpha"=>50));
		 $SplitChart->drawRadar($myPicture,$MyData,$Options);
		
		 /* Render the picture (choose the best way) */
		 $myPicture->autoOutput("pictures/example.radar.values.png");     	
    }  

    private function drawTitle($picture, $X, $Y, $Text, $Format){
    			 /* Write the picture title */ 
		 $picture->setFontProperties(array("FontName"=>$this->fontlocation . $this->fontname. '.ttf',"FontSize"=>$this->titleFontSize));
		 $picture->drawText($X, $Y, $Text,array("R"=>255,"G"=>255,"B"=>255));
    	
    }
}